package com.yzyz.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yzyz.bean.route.RouteBean;
import com.yzyz.dao.RouteDao;
import com.yzyz.dao.IScenicDao;
import com.yzyz.service.IRouteService;
import com.yzyz.utils.error.Error;

/**
 * 
 * @author David Sun
 * @Date   2010-3-14
 *
 */
@Transactional
public class RouteServiceImpl implements IRouteService {
	private static Log logger = LogFactory.getLog(RouteServiceImpl.class);
	private RouteDao routeDao;
	private IScenicDao scenicDao;
	public void setScenicDao(IScenicDao scenicDao) {
		this.scenicDao = scenicDao;
	}
	@Override
	public int add(Object object) {
		((RouteBean)object).setId(this.routeDao.save(object));
		return Error.SUCCESS;
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int delete(int id) {
		if(this.routeDao.delete(id) != 1) {
			return Error.OBJECT_NOT_EXIST;
		}
		return Error.SUCCESS;

	}

	@Override
	public Object find(int id) {
		return this.routeDao.get(id);
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int update(Object object) {
		RouteBean form = (RouteBean)object;
		RouteBean old = (RouteBean)this.routeDao.get(form.getId());
		if(old == null) {
			return Error.OBJECT_NOT_EXIST;
		}
		this.routeDao.update(object);
		return Error.SUCCESS;
	}

	@Override
	public void setRouteDao(RouteDao routeDao) {
		this.routeDao = routeDao;
	}

	@Override
	public List<RouteBean> listAllWithoutDisabled() {
		
		return this.routeDao.getAllWithoutDisabled();
	}

	@Override
	public List<RouteBean> listAllWithoutDisabled(int ownerId) {
		
		return null;
	}

	@Override
	public List<RouteBean> listAll(int start, int limit, String routeName, int groupId) {
		if(start <= 0) {
			start = 0;
		} 
		if(limit < 0) {
			limit = 15;
		}
		return this.routeDao.getAll(start, limit, routeName, groupId);
	}

	@Override
	public int getCountOfAll(String routeName, int groupId) {
		
		return this.routeDao.getCountOfAll(routeName, groupId);
	}

	@Override
	public int getCountOfWithoutDisabled() {
		return this.routeDao.getCountOfWithoutDisabled();
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int disable(int routeId) {
		int rows = this.routeDao.updateAvailable(routeId, false);
		if(rows != 1) {
			return Error.OBJECT_NOT_EXIST;
		}
		return Error.SUCCESS;
	}
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int enable(int routeId) {
		int rows = this.routeDao.updateAvailable(routeId, true);
		if(rows != 1) {
			return Error.OBJECT_NOT_EXIST;
		}
		return Error.SUCCESS;
	}

	@Override
	public List<RouteBean> listAllByScenic(int scenicId) {
		
		return this.routeDao.getAllByScenic(scenicId);
	}

	@Override
	public List<RouteBean> search(String keyWords) {
		
		return this.routeDao.search(keyWords);
	}
	@Override
	public List<RouteBean> init(int routeGroupId) {
		List<RouteBean> routes = this.routeDao.getAllWithoutDisabled(routeGroupId);
		if(routes == null){
			return null;
		}
		for(RouteBean route: routes){
			route.setScenics(this.scenicDao.getAllByRoute(route.getId()));
			logger.info(route.getName() + " has " + (route.getScenics()==null?0:route.getScenics().size()) + " scenics.");
		}
		return routes;
	}
	@Override
	public RouteBean initSingle(int routeId) {
		RouteBean route = (RouteBean)this.routeDao.get(routeId);
		if(route == null){
			return null;
		}
		route.setScenics(this.scenicDao.getAllByRoute(routeId));
		
		return route;
	}
	@Override
	public List<?> paginate(int start, int limit) {
		// TODO Auto-generated method stub
		return null;
	}

}
